/*ParallelPlatform Project - A web platform to distribute and share knowledge
Copyright (C) 2011  Daniel Guerrero Martinez & Sergio Rodriguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.*/
GENTICS.Aloha.ImgLink = new GENTICS.Aloha.Plugin("com.parallelplatform.aloha.plugins.ImgLink");
GENTICS.Aloha.ImgLink.languages = ["en", "es"];
GENTICS.Aloha.ImgLink.button = null;
GENTICS.Aloha.ImgLink.browser = null;

GENTICS.Aloha.ImgLink.init = function () {
	this.initButtons();

	$.each( $(".imageUrl") , function(){
		$(this).click( GENTICS.Aloha.ImgLink.adminImgLink );
	});
	
	GENTICS.Aloha.EventRegistry.subscribe(GENTICS.Aloha, "selectionChanged", function (e, params) {
		if( GENTICS.Aloha.activeEditable.getId() == 'title' )
			GENTICS.Aloha.ImgLink.button.hide();
		else
			GENTICS.Aloha.ImgLink.button.show();
			
		GENTICS.Aloha.FloatingMenu.doLayout();
	});
	this.browser = new GENTICS.Aloha.ui.Browser();
	this.browser.setObjectTypeFilter(['website']);
};

GENTICS.Aloha.ImgLink.initButtons = function () {
	var that = this;
	button = new GENTICS.Aloha.ui.Button({
							iconClass : "GENTICS_button imageUrlBtn",
							size : "small",
							//label: that.i18n("button.addResources"),
							onclick : function () {
								var range = GENTICS.Aloha.Selection.getRangeObject();
								if( range.isCollapsed() ){
									var Img = jQuery('<span class="imageDivUrl"><a class="imageUrl"><img /></a></span>');
									GENTICS.Utils.Dom.insertIntoDOM(Img, range, jQuery(GENTICS.Aloha.activeEditable.obj));
									Img.find("a").click( GENTICS.Aloha.ImgLink.adminImgLink );
									Img.find("a").click();
								}
							},
							tooltip : that.i18n("button.addImgLink.tooltip"),
							toggle : false
						});
	
	GENTICS.Aloha.FloatingMenu.addButton("GENTICS.Aloha.continuoustext", button, GENTICS.Aloha.i18n(GENTICS.Aloha, "floatingmenu.tab.insert"), 1);
	GENTICS.Aloha.ImgLink.button = button;
}

GENTICS.Aloha.ImgLink.uploadFile = function(form,success) {
	var options = { 
		dataType: "json",
		success: function (response, status, xhr, $form){
			if( response.success )
				success(response);
    		else
    			alert(response.error);
		},
		
		beforeSubmit: function (formData, jqForm, options){
			if (jqForm.find("input[type=file]").val() == "" ){
				alert(GENTICS.Aloha.Resources.i18n("message.uploadFailure"));
				return false;
			}
			return true;
		},
	};
	form.ajaxSubmit(options);
}

GENTICS.Aloha.ImgLink.adminImgLink = function () {
	var that = GENTICS.Aloha.ImgLink
	var ImgLink = $(this);
	GENTICS.Aloha.ImgLink.editImgLink = ImgLink
	
	var link = "/media/templates/default.png"
	if( ImgLink.find("img").attr("src") != null )
		link = ImgLink.find("img").attr("src")

	var url = "";
	if (ImgLink.attr("href") != undefined)
		var url = ImgLink.attr("href");	 
	
	var dialog = jQuery('<div class="totalPanel"><div id="dialogImgLink" class="dialogPanel">'+
			   '<p title="'+that.i18n("p.imagesInfoTitle")+'">'+ that.i18n("p.imagesInfo") +'</p>'+
			   '<div class="addImg" > <img src="'+link+'" /> '+
			   '</div> '+
			   '<form action="/resources/" method="post" enctype="multipart/form-data">'+
				 	'<input id="fileToUpload" type="file" name="file" accept="image/jpeg,image/png,image/gif" />'+
			   '</form>'+
			   '<span id="imageLinkIcon"></span>'+
			   '<div class="linkText" >'+
			   		'<p>'+that.i18n("p.linkInfo")+'</p>'+
			   		'<input name="link" type="text" value="'+url+'" />'+
			   		'<input name="browser" type="button" title="'+that.i18n("input.browser")+'" value="'+that.i18n("input.browser")+'" />'+
			   '</div>'+
               '<div class="subMenu"><input type="button" name="ok" value="'+ that.i18n("input.dialogOk") +'">'+
               '</div>'+
               '</div></div>');
	
	that.browser.onSelect = function (item) {
		$(dialog).find("input[name=link]").val(item.url);	
	};
	
	$(dialog).find("input[name=browser]").click( function(event) {
	  	that.browser.show();
	});
	
	$(dialog).find("#fileToUpload").change( function(event) {
		event.preventDefault();
		that.uploadFile($(this).parent(),function(response){
			if (response.success)
				dialog.find(".addImg > img").attr("src",response.url);
			else
				alert(response.errors);
		});
	});
	
	dialog.find('input[name=ok]').click( function(){
		var dialog = $("#dialogImgLink");
		ImgLink.attr( "href", $(dialog).find(".linkText > input[name=link]").val() )
		ImgLink.find("img").attr("src", $(dialog).find(".addImg > img").attr("src") )
		console.lo
		that.browser.win.hide()
		$(".totalPanel").fadeOut("fast", function(){ $(this).remove();});
	});
	
	
	$("body").append(dialog);
	dialog.css("display", "none");
	dialog.fadeIn("fast");
	
}


